﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类POption。
    /// </summary>
    public class POptionDAO
    {
        public POptionDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("OptionID", "POption");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int OptionID, int LanguageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = OptionID;
            parameters[1].Value = LanguageID;

            int result = DbHelperSQL.RunProcedure("UP_POption_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.POption model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@OptionName", SqlDbType.VarChar,50),
					new SqlParameter("@FieldType", SqlDbType.Int,4),
					new SqlParameter("@FieldMetaData", SqlDbType.VarChar,200)};
            parameters[0].Value = model.OptionID;
            parameters[1].Value = model.LanguageID;
            parameters[2].Value = model.OptionName;
            parameters[3].Value = model.FieldType;
            parameters[4].Value = model.FieldMetaData;

            DbHelperSQL.RunProcedure("UP_POption_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.POption model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@OptionName", SqlDbType.VarChar,50),
					new SqlParameter("@FieldType", SqlDbType.Int,4),
					new SqlParameter("@FieldMetaData", SqlDbType.VarChar,200)};
            parameters[0].Value = model.OptionID;
            parameters[1].Value = model.LanguageID;
            parameters[2].Value = model.OptionName;
            parameters[3].Value = model.FieldType;
            parameters[4].Value = model.FieldMetaData;

            DbHelperSQL.RunProcedure("UP_POption_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int OptionID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4)
                                        };
            parameters[0].Value = OptionID;

            DbHelperSQL.RunProcedure("UP_POption_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.POption GetModel(int OptionID, int LanguageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@OptionID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = OptionID;
            parameters[1].Value = LanguageID;

            TimKen.DataEntity.POption model = new TimKen.DataEntity.POption();
            DataSet ds = DbHelperSQL.RunProcedure("UP_POption_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["OptionID"].ToString() != "")
                {
                    model.OptionID = int.Parse(ds.Tables[0].Rows[0]["OptionID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LanguageID"].ToString() != "")
                {
                    model.LanguageID = int.Parse(ds.Tables[0].Rows[0]["LanguageID"].ToString());
                }
                model.OptionName = ds.Tables[0].Rows[0]["OptionName"].ToString();
                if (ds.Tables[0].Rows[0]["FieldType"].ToString() != "")
                {
                    model.FieldType = int.Parse(ds.Tables[0].Rows[0]["FieldType"].ToString());
                }
                model.FieldMetaData = ds.Tables[0].Rows[0]["FieldMetaData"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct OptionID,LanguageID,OptionName,FieldType,FieldMetaData");
            strSql.Append(" FROM POption ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetOptionByType(int pagesize,int pageindex,string typeID,string languageID)
        {

            SqlParameter[] parameters = {
                  
                  
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                   
                    new SqlParameter("@CategoryID", SqlDbType.Int),
                     new SqlParameter("@LanguageID", SqlDbType.Int)
                   
                    };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = typeID;
            parameters[3].Value = languageID;
            return DbHelperSQL.RunProcedure("UP_POptionGetOptionByCategory", parameters, "ds");
        }
        public DataSet GetOptionNotInType(int pagesize, int pageindex, string typeID, string languageID,string fieldType,string attrName)
        {

            SqlParameter[] parameters = {
                  
                  
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@CategoryID", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int),
                    new SqlParameter("@FieldType",SqlDbType.Int),
                    new SqlParameter("@AttrName",SqlDbType.VarChar,50)
                    };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = typeID;
            parameters[3].Value = languageID;
            parameters[4].Value = fieldType;
            parameters[5].Value = attrName;
            return DbHelperSQL.RunProcedure("UP_POptionGetOptionNotInCategory", parameters, "ds");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetPageList(int pagesize, int pageindex,  string languageID, string fieldType, string optionName)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int),
                    new SqlParameter("@FieldType",SqlDbType.Int),
                    new SqlParameter("@OptionName",SqlDbType.VarChar,50)
                    };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = languageID;
            parameters[3].Value = fieldType;
            parameters[4].Value = optionName;
            return DbHelperSQL.RunProcedure("UP_POption_GetPageList", parameters, "ds");
        }



        public DataSet GetSearchableListByCategory(int categoryID,int languageID,string where,ref int productCount,int brandId)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@CategoryID", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int),
                    new SqlParameter("@Where",SqlDbType.VarChar),
                    new SqlParameter("@flag",SqlDbType.Int),
                    new SqlParameter("@brandId",SqlDbType.Int)
                    };
            parameters[0].Value = categoryID;
            parameters[1].Value = languageID;
            parameters[2].Value = where;
            parameters[3].Direction = ParameterDirection.Output;
            parameters[4].Value = brandId;

           DataSet  ds =  DbHelperSQL.RunProcedure("UP_POption_GetSearchableListByCategory", parameters, "ds");

           productCount = (int)parameters[3].Value;


           return ds;
        }


        public long GetListCount(string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "POption";
            parameters[1].Value = "OptionID";
            parameters[2].Value = 1;
            parameters[3].Value = 0;
            parameters[4].Value = strWhere;
            SqlDataReader dr = DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters);
            if (dr.Read())
            {
                long count =  Convert.ToInt64(dr[0]);

                dr.Close();
                dr.Dispose();

                return count;
            }
            return 0;

        }
 
        #endregion  成员方法
    }
}
